﻿using System;
using System.IO;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Eugenhome.Atrium.Bookmark.Web
{
    /// <summary>
    /// 
    /// </summary>
    public partial class BookmarkCry : System.Web.UI.Page
    {
        /// <summary></summary>
        private Login _login = new Login();

        /// <summary></summary>
        public string SelectedFileName { get; set; }
        /// <summary></summary>
        public string SelectedFileContent { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            _login.Check();

            SelectedFileName = lstFiles.SelectedValue;

            if (!IsPostBack)
            {
                SelectedFileName = "";

                //Server-Files aktualisieren und anzeigen
                UpdateServerFiles();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private void UpdateServerFiles()
        {
            lstFiles.DataSource = new BookmarkLinkFile("").GetFiles(Server.MapPath(""));
            lstFiles.DataTextField = "DisplayName";
            lstFiles.DataValueField = "FullFileName";
            lstFiles.DataBind();

            if (SelectedFileName.Equals(string.Empty) || !lstFiles.Items.Cast<ListItem>().Any(item => item.Value.Equals(SelectedFileName)))
            {
                SelectedFileName = lstFiles.Items.Count.Equals(0) ? "" : lstFiles.Items[0].Value;
            }

            if (!SelectedFileName.Equals(string.Empty))
            {
                lstFiles.SelectedValue = SelectedFileName;
                lstFiles_SelectedIndexChanged(null, null);
            }
            else
            {
                txtContent.Text = "";
                txtFileName.Text = "";
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lstFiles_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (File.Exists(lstFiles.SelectedValue))
            {
                SelectedFileName = new BookmarkLinkFile(lstFiles.SelectedValue).DisplayName;

                SelectedFileContent = _login.DecryptContent(lstFiles.SelectedValue);

                this.DataBind();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveFile_Click(object sender, ImageClickEventArgs e)
        {
            if (lstFiles.SelectedValue != null && !txtFileName.Text.Trim().Equals(string.Empty))
            {
                string selectedFile = lstFiles.SelectedValue;

                string dir = Path.GetDirectoryName(selectedFile);
                string newFileName = Path.Combine(dir, txtFileName.Text);

                if (File.Exists(newFileName))
                {
                    try
                    {
                        File.Delete(newFileName);
                    }
                    catch { }
                }

                File.WriteAllText(newFileName, txtContent.Text);

                _login.EncryptFile(newFileName);

                UpdateServerFiles();

                lstFiles.SelectedValue = newFileName;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnNewFile_Click(object sender, ImageClickEventArgs e)
        {
            txtFileName.Text = "";
            txtContent.Text = "";
            btnDeleteFile.Enabled = false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDeleteFile_Click(object sender, ImageClickEventArgs e)
        {
            if (lstFiles.SelectedValue != null && File.Exists(lstFiles.SelectedValue))
            {
                try
                {
                    File.Delete(lstFiles.SelectedValue);
                }
                catch { }

                UpdateServerFiles();

                if (!lstFiles.Items.Count.Equals(0))
                {
                    lstFiles.SelectedIndex = 0;
                }
                else
                {
                    txtFileName.Text = "";
                    txtContent.Text = "";
                }
            }
        }

    }
}